package com.example.rice.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.rice.common.R;
import com.example.rice.entity.Employee;
import com.example.rice.service.EmployeeService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/employee")
//用户控制器
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;


    /**
     * 新增员工
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Employee employee){
        log.info("新增的员工信息{}",employee);
        //设置初始密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employeeService.save(employee);
        return R.success("新增员工成功！");
    }


    /**
     * 登录功能控制器
     * @param employee
     * @param request
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request){
        //对密码进行加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //进行数据库查询
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);
        if (emp == null){
            return R.error("不存在该用户");
        }
        //进行密码比对
        if (!password.equals(emp.getPassword())){
            return R.error("密码错误！");
        }
        //进行禁用比对
        if (emp.getStatus() == 0){
            return R.error("该账号已禁用");
        }
        request.getSession().setAttribute("employee",emp.getId());
        return  R.success(emp);
    }


    /**
     * 员工退出
     * */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }


    /**
     * 分页查询员工信息
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name){
        log.info("{},{},{}",page,pageSize,name);
        //构造分页构造器
        Page pageInfo = new Page<>(page,pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        //进行分页查询
        employeeService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

    /**
     * 修改员工信息
     * @param
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> udpdate(@RequestBody Employee employee){
        employeeService.updateById(employee);
        return R.success("成功更改账号状态");
    }

    /**
     * 根据id查询员工信息
     * 使用PathVariable注解时需要与路径中的变量名一致
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R<Employee> getOneEmployee(@PathVariable("id") Long id){
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }
}
